#include "ToolSystem.h"
#include "CoreSystem.h"
#include "Graph.h"

#include <fstream>
#include <vector>
#include <algorithm>



#pragma warning( disable : 4100 )

bool ToolSystem::Initialize(void* hwnd)
///////////////////////////////////////////////////////////////////////////////
// Description: 
//
// Recent Changes:
// -brasmuss 4/10/2007 : Created
///////////////////////////////////////////////////////////////////////////////
{
	m_core = Audio::CoreSystem::Inst();
	if(!m_core->Initialize((HWND)hwnd, true))
		return false;

	// Create XML data to work with
	m_xmlHandle = m_core->LoadBlankXML(DEFAULT_XML_FILE);
	return true;
}

bool ToolSystem::Destroy()
///////////////////////////////////////////////////////////////////////////////
// Description: 
//
// Recent Changes:
// -brasmuss 4/10/2007 : Created
///////////////////////////////////////////////////////////////////////////////
{
	m_core->Destroy();
	m_core = NULL;
	return true;
}

void ToolSystem::Update(float delta)
{
	m_core->Update(delta);
}

bool ToolSystem::LoadSoundXML( const char* file )
///////////////////////////////////////////////////////////////////////////////
// Description: 
//
// Recent Changes:
// -brasmuss 4/10/2007 : Created
///////////////////////////////////////////////////////////////////////////////
{
	m_xmlHandle = m_core->LoadSoundXML(file);
	return m_xmlHandle ? true : false;
}

bool ToolSystem::SaveSoundXML( const char* file )
///////////////////////////////////////////////////////////////////////////////
// Description: 
//
// Recent Changes:
// -brasmuss 4/10/2007 : Created
///////////////////////////////////////////////////////////////////////////////
{
	return m_core->SaveSoundXML(m_xmlHandle,file);
}

void ToolSystem::Clear()
{
	m_core->Clear();
	
	// Create XML data to work with
	m_xmlHandle = m_core->LoadBlankXML(DEFAULT_XML_FILE);
}

// Sound file management
FileHandle  ToolSystem::GetSoundFileHandle(bool first)
{
	return m_core->GetSoundFileHandle(first, m_xmlHandle);
}

FileHandle ToolSystem::NewSoundFile(const char* name, const char* file)
{
	return m_core->NewSoundFile(name,file,m_xmlHandle);
}

const char* ToolSystem::GetSoundFilename(const FileHandle& handle)
{
	return m_core->GetSoundFilename(handle,m_xmlHandle);
}

const char* ToolSystem::GetSoundName(const FileHandle& handle)
{
	return m_core->GetSoundName(handle,m_xmlHandle);
}

bool ToolSystem::SetSoundFilename(const FileHandle& handle, const char* filename)
{
	return m_core->SetSoundFilename(handle,filename,m_xmlHandle);
}

bool ToolSystem::SetSoundName(const FileHandle& handle, const char* name)
{
	return m_core->SetSoundName(handle,name,m_xmlHandle);
}

bool ToolSystem::RemoveSound(const FileHandle& handle)
{
	return m_core->RemoveSound(handle,m_xmlHandle);
}
unsigned ToolSystem::GetSoundFreq(const FileHandle& handle)
{
	return m_core->GetSoundFreq(handle);
}
unsigned ToolSystem::GetSoundBitDepth(const FileHandle& handle)
{
	return m_core->GetSoundBitDepth(handle);
}
unsigned ToolSystem::GetSoundChannels(const FileHandle& handle)
{
	return m_core->GetSoundChannels(handle);
}
int ToolSystem::GetSoundLength(	const FileHandle& handle )
{
	return m_core->GetSoundLength( handle );
}
// Sound path management
bool ToolSystem::SetSoundPath(const char* path)
{	
	return m_core->SetSoundPath(path,m_xmlHandle);
}

const char* ToolSystem::GetSoundPath()
{
	return m_core->GetSoundPath(m_xmlHandle);
}

// Variable management
VariableHandle	ToolSystem::GetVariableHandle(	bool first )
{
	return m_core->GetVariableHandle(first, m_xmlHandle);
}
VariableHandle ToolSystem::NewVariable(const char* varName, float min, float max)
{
	return m_core->NewVariable(varName,min,max,m_xmlHandle);
}

const char* ToolSystem::GetVariableName(const VariableHandle& handle)
{
	return m_core->GetVariableName(handle,m_xmlHandle);
}

float ToolSystem::GetVariableMin(const VariableHandle& handle)
{
	return m_core->GetVariableMin(handle,m_xmlHandle);
}

float ToolSystem::GetVariableMax(const VariableHandle& handle)
{
	return m_core->GetVariableMax(handle,m_xmlHandle);
}

float ToolSystem::GetVariableValue(VariableHandle& handle)
{
	return m_core->GetVariableValue(handle,m_xmlHandle);
}

bool ToolSystem::SetVariableName(const VariableHandle& handle, const char* name)
{
	return m_core->SetVariableName(handle,name,m_xmlHandle);
}

bool ToolSystem::SetVariableMin(const VariableHandle& handle, float min)
{
	return m_core->SetVariableMin(handle,min,m_xmlHandle);
}

bool ToolSystem::SetVariableMax(const VariableHandle& handle, float max)
{
	return m_core->SetVariableMax(handle,max,m_xmlHandle);
}

bool ToolSystem::SetVariableValue(const VariableHandle& handle, float value)
{
	return m_core->SetVariableValue(handle,value,m_xmlHandle);
}

bool ToolSystem::RemoveVariable(const VariableHandle& handle)
{
	return m_core->RemoveVariable(handle,m_xmlHandle);
}


// Graph management
GraphHandle ToolSystem::GetGraphHandle(bool first)
{
	return m_core->GetGraphHandle(first, m_xmlHandle);
}
GraphHandle ToolSystem::NewGraph( const char* graphName )
{
	return m_core->NewGraph(graphName,m_xmlHandle);
}

bool ToolSystem::ClearGraph( const GraphHandle& graphID )
{
	return m_core->ClearGraph(graphID,m_xmlHandle);
}

bool ToolSystem::RemoveGraph( const GraphHandle& graphID )
{
	return m_core->RemoveGraph(graphID,m_xmlHandle);
}

const char*	ToolSystem::GetGraphName( const GraphHandle& graphID )
{
	return m_core->GetGraphName(graphID,m_xmlHandle);
}

bool ToolSystem::SetGraphName( const GraphHandle& graphID, const char* name )
{
	return m_core->SetGraphName(graphID,name,m_xmlHandle);
}

bool ToolSystem::AddPoint( const GraphHandle& graphID, float x, float y)
{
	return m_core->AddPoint(graphID,x,y,m_xmlHandle);
}
unsigned ToolSystem::GetPointCount( const GraphHandle& graphID )
{
	return m_core->GetPointCount(graphID, m_xmlHandle);
}
float ToolSystem::GetPointX( const GraphHandle& graphID, const unsigned index )
{
	return m_core->GetPointX(graphID, index, m_xmlHandle);
}
float ToolSystem::GetPointY( const GraphHandle& graphID, const unsigned index )
{
	return m_core->GetPointY(graphID, index, m_xmlHandle);
}

// Events, SubEvents & Effects
EventHandle ToolSystem::GetEventHandle( bool first )
{
	return m_core->GetEventHandle( first, m_xmlHandle );
}
EventHandle	ToolSystem::NewEvent( const char* name, bool looping )
{
	return m_core->NewEvent(name,looping,m_xmlHandle);
}

bool ToolSystem::RemoveEvent( const EventHandle& eventID )
{
	return m_core->RemoveEvent(eventID,m_xmlHandle);
}

SubEventHandle ToolSystem::AddSubEvent( const EventHandle& eventID, const char* soundName, float triggerTime, int pitchShift, bool streaming )
{
	return m_core->AddSubEvent(eventID,soundName,triggerTime,pitchShift,streaming,m_xmlHandle);
}

bool ToolSystem::RemoveSubEvent( const EventHandle& eventID, const SubEventHandle& subID )
{
	return m_core->RemoveSubEvent(eventID,subID,m_xmlHandle);
}

EffectHandle ToolSystem::AddSoundEffect( const EventHandle& eventID, const SubEventHandle& subID, const char* graphName, const char* varName, unsigned effect )
{
	return m_core->AddSoundEffect(eventID,subID,graphName,varName,effect,m_xmlHandle);
}

EffectHandle ToolSystem::AddSoundEffect( const EventHandle& eventID, const SubEventHandle& subID, const GraphHandle& graphID, const VariableHandle& varID, unsigned effect )
{
	return m_core->AddSoundEffect(eventID,subID,graphID,varID,effect,m_xmlHandle);
}

bool ToolSystem::RemoveSoundEffect( const EventHandle& eventID, const SubEventHandle& subID, const EffectHandle& effect )
{
	return m_core->RemoveSoundEffect(eventID,subID,effect,m_xmlHandle);
}
const char*	ToolSystem::GetEventName(const EventHandle& eventID )
{
	return m_core->GetEventName(eventID, (const XMLFileHandle&)m_xmlHandle);
}
bool ToolSystem::GetEventLooping( const EventHandle& eventID )
{
	return m_core->GetEventLooping( eventID,m_xmlHandle );
}
SubEventHandle ToolSystem::GetSubEventHandle(const EventHandle& eventID, bool first)
{
	return m_core->GetSubEventHandle(eventID, first, m_xmlHandle);
}
const char* ToolSystem::GetSoundName(const EventHandle& eventID, const SubEventHandle& subID)
{
	return m_core->GetSoundName(eventID, subID, m_xmlHandle);
}
float ToolSystem::GetTriggerTime(const EventHandle& eventID, const SubEventHandle& subID)
{
	return m_core->GetTriggerTime(eventID, subID, m_xmlHandle);
}
int	ToolSystem::GetPitchShift(const EventHandle& eventID, const SubEventHandle& subID)
{
	return m_core->GetPitchShift(eventID, subID, m_xmlHandle);
}
bool ToolSystem::GetStreaming(		const EventHandle& eventID, const SubEventHandle& subID )
{
	return m_core->GetStreaming(eventID, subID, m_xmlHandle);
}

EffectHandle ToolSystem::GetEffectHandle(const EventHandle& eventID, const SubEventHandle& subID, bool first)
{
	return m_core->GetEffectHandle(eventID, subID, first, m_xmlHandle);
}
GraphHandle ToolSystem::GetEffectGraph(const EventHandle& eventID, const SubEventHandle& subID, const EffectHandle& fxID)
{
	return m_core->GetEffectGraph(eventID, subID, fxID, m_xmlHandle);
}
VariableHandle ToolSystem::GetEffectVariable(const EventHandle& eventID, const SubEventHandle& subID, const EffectHandle& fxID)
{
	return m_core->GetEffectVariable(eventID, subID, fxID, m_xmlHandle);
}
unsigned ToolSystem::GetEffectType(const EventHandle& eventID, const SubEventHandle& subID, const EffectHandle& fxID)
{
	return m_core->GetEffectType(eventID, subID, fxID, m_xmlHandle);
}
